//
// Created by kenny on 25-11-11.
//
#include <stdint.h>
#include <stdbool.h>
#include "mcp_msg_queue.h"
#include "../bm/sys_inc.h"
#include "../mcp_logger.h"

msg_queue *live_que_v;
int st_TimeoutLiveV = 0;

void * streamVideoThread(void * argv) {
    int sret = -1;
    int uaf_rtp_tx = 1;
    // UA_PACKET packet;
    // double framerate = p_sua->media_info.framerate;
    // int fdIndex=p_sua->video_rtp_media.remote_port%5;
    int nSize=0;
    char* encoder = "PS";
    while (uaf_rtp_tx) {
        {
            nSize = mq_get_msg_size_que_used(live_que_v);
        }
        if (nSize > 0) {
            st_TimeoutLiveV=0;
            memory_block *p_memory_block = NULL;
            {
                p_memory_block = mq_get_msg_que_used(live_que_v);
            }
            if (p_memory_block != NULL) {
                //log_print(LOG_DBG, "streamVideoThread  thread[live]    ~~~~~~~ size:%d\n",pMemBlock->GetRealBufSize());
                int retps = 0;
                // PS 流
                if (strcasecmp(encoder, "PS") == 0) {

                    retps = rtp_ps_video_tx(p_sua, pMemBlock->GetBufHead() + 256,
                                            pMemBlock->GetRealBufSize() - 256,
                                            streamGetTimestamp3(1, framerate,p_sua->v_rtp_info.rtp_ts));
                }

                // free(packet.buff);
                if (retps < 0) {
                    log_print(LOG_DBG, "streamVideoThread live [%d] 退出流线程....\n", retps);
                    _st_liveQueV.SubmitMessageQueUnuse(pMemBlock);
                    _st_liveQueV.Destroy();
                    _st_liveQueA.Destroy();
                    p_sua->video_rtp_media.ua_m_fd = -1;
                    p_sua->media_info.v_index = 0;
                    p_sua->media_info.a_index = 0;
                    liveaudiovideo_stopStream(p_sua);
                    break;
                }
                _st_liveQueV.SubmitMessageQueUnuse(pMemBlock);
            }
        } else {
           // log_print(LOG_DBG, "streamVideoThread  GetMsgSizeQueUsed==0 wait 10ms....\n");
            usleep(10 * 1000);
            st_TimeoutLiveV++;
            //if(p_sua->rtp_thread<=0) {
                log_print(LOG_DBG, "streamVideoThread  GetMsgSizeQueUsed==0 buffer thread timeout:%d end...\n",st_TimeoutLiveV);
             if(st_TimeoutLiveV>400)
                break;
           // }
        }
    }
    p_sua->media_info.v_thread = 0;
    p_sua->uaf_rtp_tx=0;
    log_print(LOG_DBG,"vvvvvvvvvvvvvvvvvvvvvvvv streamVideoThread video_thread end=[%d] vvvvvvvvvvvvvvvvvvvvvvvv\n",pthread_self());
    return NULL;
}
void liveaudiovideo_thread()
{
    pthread_t  rtp_thread=0;
    int is_exit_flag = 0;  //是否退出服务标识
    int uaf_rtp_tx = 1;        // RTP thread starts, is sending RTP
    int vcodec=1;              // video codec
    int width=1280;            // video width
    int height720;             // video height
    int framerate=25;          // video frame rate
    int samplerate=8000;       // audio sample rate
    //创建一个 视频 队列
    rtp_thread = sys_os_create_thread((void *)streamVideoThread, NULL);
    while (uaf_rtp_tx)
    {
        if(is_exit_flag > 0) //准备退出当前服务
        {
            LOG_DEBUG("---实时视频线程-准备退出当前服务-------- \n");
            break;
        }
        usleep(10*1000);
    }
    // 关闭线程
    // p_sua->rtp_thread = 0;
    // 停止socket
    // sua_stop_media(p_sua);    //停止 媒体通道
    // streamFreeQueue(p_sua);
    // gLiveaudiovideo.Fd = NULL; //置空句柄
    LOG_DEBUG("[[liveaudiovideo_thread]]>>>>=======[updateState_liveState]======STATE_STOP\n");
}

void stream_put_video(uint8_t *data, int size, int waitnext )
{
    if (!live_que_v) {
        live_que_v = mq_create();
    }
    memory_block *p_memory_block = NULL;
    {
        // 取得一个未使用的 block
        p_memory_block = mq_get_msg_que_unuse(live_que_v);
    }

    if (p_memory_block != NULL) {
        if (data && size > 0) {
            unsigned  char szTmp[256]="";
            // 先再一个 256b 到block
            mb_copy_data(p_memory_block, szTmp,256);
            // 再把 data 加到 block 之后
            mb_add_copy_data(p_memory_block,data,size);
            // 提交到已使用块
            mq_submit_msg_que_used(live_que_v,p_memory_block);
        }
    }
}

//实时视频 --- 传入视频 音频   DATA_TYPE_AUDIO =0 ,DATA_TYPE_VIDEO =1
